if(!dojo._hasResource["opm.widgets.component.Group"]){
dojo._hasResource["opm.widgets.component.Group"] = true;
dojo.provide("opm.widgets.component.Group");
dojo.require("dijit._Widget");
dojo.require("opm.widgets.Ajax");
dojo.require("opm.widgets.GenericForm");
dojo.declare("opm.widgets.component.Group",dijit._Widget,
{
   contentId:"",
   defaultURL:"",
   _GENERIC_USER_ADD_FORM:"opm.widgets.Group.User.addForm",
   _GENERIC_USER_GROUP:"opm.widgets.Group.User.group",
   _GENERIC_USER_REVIEW_FORM:"opm.widgets.Group.User.reviewForm",
   _GENERIC_ADD_FORM:"opm.widgets.Group.addForm",
   _GENERIC_EDIT_FORM:"opm.widgets.Group.editForm",
   
   constructor:function(args){
      this.setAtts(args);
   },
   setAtts:function(args){
      this.contentId = args.contentId;
   },
   show:function(){
      var args = {ajaxUrl:baseAuth+'/sec/group/list',callback: this._initSecs};
      OPM.ajaxWidget(args,this.contentId).callXhrGet();
   },
   showUser:function(){
      var args = {ajaxUrl:baseAuth+'/sec/user/list',callback: this._initUser};
      OPM.ajaxWidget(args,this.contentId).callXhrGet();
   },
   showAddForm:function(){
      var dataform = {
         ajaxCallUrl: baseAuth+'/sec/group/create', 
         ajaxCallType:'callXhrGetJson',
         contentId: this.contentId,
         widgetId: this._GENERIC_ADD_FORM
      };
      var genericForm = this.commonForm(dataform);
      genericForm.createForm();
   },
   showAddUser:function(){
      var args = {ajaxUrl:baseAuth+'/sec/user/create',callback: this._initAddUserPage};
      OPM.ajaxWidget(args, this.contentId).callXhrGet();
   },
   showEditForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: this.contentId,
         widgetId: this._GENERIC_EDIT_FORM
      };
      var genericForm = this.commonForm(dataform);
      genericForm.createForm();
   },
   showEditUserForm:function(ajaxUrl){
      var args = {ajaxUrl:ajaxUrl,callback: this._initAddUserPage};
      OPM.ajaxWidget(args, this.contentId).callXhrGet();
   },
   commonForm:function(dataform){
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
         OPM.groupWidget().show();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(){
            OPM.groupWidget().show();
         }});
      });
      return genericForm;
   },
   _initSecs:function(){
      dojo.query('#hrefViewAddGroup').onclick(function(){
         OPM.groupWidget().showAddForm();
      });

      dojo.query('table#group tbody tr').connect('ondblclick', function(evt){
         var editUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.groupWidget().showEditForm(editUrl);
      });
   },
   _initUser:function(){
      dojo.query('#hrefViewAddUser').onclick(function(){
         OPM.groupWidget().showAddUser();
      });

      dojo.query('table#user tbody tr').connect('ondblclick', function(evt){
         var editUrl = dojo.query('input[type="hidden"]', this)[0].value;
         OPM.groupWidget().showEditUserForm(editUrl);
      });
   },
   _initAddUserPage:function(){
      var descriptions = dojo.byId('titleDescription').value.split("||");
      var properties = dojo.byId('titleGroup').value.split("||");
      var review = dojo.byId('titleReview').value.split("||");
      var hiddenIsEditClassDefPage = dojo.byId('isEditUserPage');
      var isEditClassDefPage = (hiddenIsEditClassDefPage != null && hiddenIsEditClassDefPage.value == "true")?false:true;
      stackObject = new opm.widgets.GenericStack({
        data:[{title:descriptions[0], href:descriptions[1], type:'description'},
              {title:properties[0], href:properties[1], type:'groups'},
              {title:review[0], href:review[1], type:'review'}],
        contentId:'stackMenuDiv', hasDisabledStackInactiveYet:isEditClassDefPage});
      stackObject.connectEvent("selectedItem",function (item){
         if (item.type == 'description' ){
            OPM.groupWidget()._showAddForm(item.href);
         }else if (item.type == 'groups'){
            OPM.groupWidget()._showGroups(item.href);
         }else if (item.type == 'review'){
            OPM.groupWidget()._showReviewForm(item.href);
         }
      });
      stackObject.generic();
   },
   _showAddForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_USER_ADD_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
         OPM.groupWidget().showUser();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(){
            stackObject.gotoNextItem();
         }});
      });
      genericForm.createForm();
   },
   _showGroups:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_USER_GROUP
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
         OPM.groupWidget().showUser();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveProperties(["JSONPropertyIds"],{handleSuccess:function(){
            stackObject.gotoNextItem();
         }});
      });
      genericForm.createForm();
   },
   _showReviewForm:function(ajaxUrl){
      var dataform = {
         ajaxCallUrl: ajaxUrl, 
         ajaxCallType:'callXhrGetJson',
         contentId: 'contentStack',
         widgetId: this._GENERIC_USER_REVIEW_FORM
      };
      var genericForm = OPM.genericFormWidget(dataform);
      genericForm.connectEvent("btnCancelClick", function(data){
         OPM.groupWidget().showUser();
      });
      genericForm.connectEvent("btnAddClick", function(data){
         genericForm.callSaveForm({handleSuccess:function(){
            OPM.groupWidget().showUser();
         }});
      });
      genericForm.createForm();
   }
});}
